from faker import Faker
import random
import time
import os
import datetime

browser_slices = ['Chrom','Chrom','Chrom','Chrom','Chrom','     ',
             'FireFox','FireFox','FireFox',
             'IE','IE','IE',
             'Sarfari','Sarfari',
             'Other']
def sample_browser():
    slice = random.choice(browser_slices)
    return slice

sort_slices = ['服装','服装','服装','服装','服装','服装','服装','服装','服装','服装',
               '食品','食品','食品','食品','食品','食品',
               '家电','家电',
               '美妆','美妆','美妆','美妆','美妆','美妆','美妆','美妆',
               '医疗',
               '图书','图书','图书',
               '工业',
               '运动','运动','运动','运动']
def sample_sort():
    slice = random.choice(sort_slices)
    return slice

def generate_log(count=100, filepath="path", filename="name"):
    a=str(count)
    log_path = os.path.join(os.getcwd(), filepath)
    log_name = os.path.join(log_path, filename)
    if not os.path.exists(log_path):
        os.mkdir(log_path)
        print(log_path, "已创建")
    elif os.path.exists(log_path) and os.path.exists(log_name):
        print(log_name, "已存在")
    with open(log_name, "a+") as f:
        while count >= 1:
            fa = Faker('zh_CN')
            log_format = "{username}\t{IP}\t{deep}\t{sort}\t{suctime}\t{adress}\t{browser}"
            query_log = log_format.format(
                username=fa.user_name(),  # 用户名
                IP=fa.ipv4(),#IP地址
                deep = fa.random_digit_not_null(),#访问深度
                sort = sample_sort(),#商品种类
                suctime=fa.time(),#成交时间
                adress=fa.province()+fa.city()+fa.district(),  # 地址
                browser = sample_browser(),#使用的浏览器
            )
            f.write(query_log + "\n")
            count = count - 1;
    print("数据生成完毕，本次共生成"+a+"条数据。")


if __name__ == '__main__':
    start = datetime.datetime.now()
    # print("开始时间："+str(datetime.datetime.now()))
    print("开始时间：" + str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))))
    print("正在执行...")
    generate_log(count=100000, filepath="data", filename="shuju.txt")
    end = datetime.datetime.now()
    print("结束时间："+str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))))
    print("共用时"+str(end - start))
